package com.sxt.dao;

import com.sxt.common.Constant;
import com.sxt.common.PageInfo;
import com.sxt.pojo.Customer;

import java.util.Date;
import java.util.Map;
import java.util.StringJoiner;

public class CustomerDao extends BaseDao{

    /**
     * 分页查询信息
     * @param params
     * @param pageNo
     * @param pageSize
     * @return
     */
    public PageInfo<Customer> selectPage(Map<String, String> params, Integer pageNo, Integer pageSize) {
        //INSERT INTO `cms`.`customer`(`id`, `name`, `phone`, `sex`, `company`, `deleted`, `user_id`, `create_time`, `update_time`, `delete_time`)
        String sql = "select c.id, c.name, c.phone, c.sex, c.company, c.deleted, c.user_id as userId, c.create_time as createTime, c.update_time as updateTime, c.delete_time as deleteTime ,u.realname from customer c LEFT JOIN user u ON c.user_id = u.id where 1=1  ";
        // 分隔符    前缀  后缀
        StringJoiner str = new StringJoiner(" and  ");
        str.add(sql); // 项1 and 项2
        if (params.containsKey("name")){
            String name = params.get("name");
            str.add("  c.name like '%"+name+"%' ");
        }
        if (params.containsKey("phone")){
            String phone = params.get("phone");
            str.add("  c.phone  ="+phone);
        }
        if (params.containsKey("sex")){
            String sex = params.get("sex");
            str.add("  c.sex  ="+sex);
        }
        if (params.containsKey("company")){
            String company = params.get("company");
            str.add("  c.company like '%"+company+"%' ");
        }
        if (params.containsKey("deleted")){
            String deleted = params.get("deleted");
            str.add("  c.deleted  ="+deleted);
        }
        if (params.containsKey("userId")){
            String userId = params.get("userId");
            str.add("  c.user_id  ="+userId);
        }
        sql = str.toString();
        System.out.println("sql为:"+sql);
        return  super.selectPage(sql, Customer.class,pageNo,pageSize);
    }

    /**
     * 根据手机号查询客户
     * @param phone
     * @return
     */
    public Customer selectByPhone(String phone) {
        String sql = "select id from customer where phone = ? ";
        return super.selectOne(sql,Customer.class,phone);
    }

    /**
     * 新增客户信息
     * @param name
     * @param phone
     * @param sex
     * @param company
     * @param id
     */
    public void insert(String name, String phone, String sex, String company, Integer id) {
        String sql = "insert into customer (name,phone,sex,company,deleted,user_id) value (?,?,?,?,?,?) ";
        super.update(sql,name,phone,sex,company,Constant.CUSTOMER_DELETE_NOT,id);
    }

    /**
     * 修改客户信息
     * @param name
     * @param phone
     * @param sex
     * @param company
     * @param id
     * @return
     */
    public Integer update(String name, String phone, String sex, String company,String id) {
        String sql = "update customer set name = ?, phone = ?, sex = ?, company = ? WHERE id = ?";
        return super.update(sql, name,phone,sex,company,id);
    }

    /**
     * 修改客户状态 -- 删除
     * @param id
     * @param deleted
     */
    public void updateState(String id, Integer deleted) {
        String sql = "update customer set deleted = ? ,delete_time = ? where id = ? ";
        super.update(sql,deleted,new Date(),id);
    }

    /**
     * 修改客户的业务员
     * @param userId
     * @param cIds
     */
    public void updateSalesman(String userId, String... cIds) {//1,2,3,4
        //两个选择
        //1.Jdbc 批量操作
        //2.组合一个SQL
        String customerIds = String.join(",",cIds);
        String sql = "update customer set user_id = ? where id in("+customerIds+")";
        super.update(sql,userId);
    }
}
